import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'
// 让vue框架应用vuex
Vue.use(Vuex)

export default ({
    state: {
        // 定义属性
        newHouse: [], //新房源列表
        houseTotal: 0, //数组总数
    },
    mutations: {
        // 给上面设置的属性变量设置
        setNewHouse(state, val) {
            //通过这个方法，修改bestHouse的值
            state.newHouse = state.newHouse.concat(val);
        },
        clearNewHouse(state) {
            state.newHouse = [] //
        }
    },
    actions: {
        // 定义方法
        getNewHouse({
            commit,
            state
        }, hpn) {
            let start = (hpn - 1) * 3
            let end = start + 3
            axios({
                method: 'get',
                url: '/data/house_data.json?hpn=1'
            }).then((res) => {
                // console.log(res.data)
                let data = res.data
                if (data.code === 200) {
                    commit('setNewHouse', data.newHouse.slice(start, end)) //获取并拼接房源列表
                    state.houseTotal = data.newHouse.length //记录当前列表程度
                    console.log("home中的", state.newHouse);
                }
            }).catch((err) => {
                console.log(err)
            })
        }
    }
})